// pages/test4/test4.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    minutes: '05',//倒计时 分
    seconds: '00',//倒计时 秒
    question: 0,
    id: "",
    answer: "",
    questionList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var info = decodeURIComponent(options.questionList);
    var questionList = JSON.parse(info);
    this.setData({
      questionList: questionList
    })
    console.log(questionList);

    this.countdown()
  },



  countdown() {
    var minute = this.data.minutes;
    var second = this.data.seconds;
    if (second === '00') { //如果秒降到00，则分减一，秒调整为59
      second = 59;
      minute = Number(minute) - 1;
      minute = minute <= 9 ? '0' + minute : minute;//为了构造两位数显示
    } else {
      second = Number(second) - 1;
      second = second <= 9 ? '0' + second : second;//为了构造两位数显示
    }
    const self = this;
    setTimeout(function () {
      self.setData({
        minutes: minute,
        seconds: second
      })
      if (second === '00' && minute === '00') { //倒计时结束
        console.log('答题时间到')
        wx.switchTab({
          title: '答题结束',
          url: '../home/home'
        });
      } else {
        self.countdown();//继续调用自己
      }
    }, 1000)
  },
  laternext: function (e) {
    // console.log(e);
    var val = e.detail.value;//获取radio值，类型：字符串

    this.setData({
      answer: val
    })
    console.log(val);

  },

  formSubmit: function (e) {


    var answer = this.data.answer;
    var id = e.detail.value.id;


    wx.showModal({
      title: '提示',
      content: '确定要提交吗?',

      success(res) {
        if (res.confirm) {
          wx.request({
            url: "http://localhost:8080/exam/wxanswer",
            data: {
              'id': id,
              'answer': answer
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              console.log(res);
              console.log(res.data);
              console.log(res.data.answer);
              if (res.data.answer == answer) {

                wx.request({

                  url: "http://localhost:8080/exam/wxExam_Type",
                  data: {
                    'question_type': "选择题"
                  },
                  method: "post",

                  header: {
                    // 'content-type': 'application/x-www-form-urlencoded'
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    console.log("已执行。。")
                    console.log(res.data.question);
                    if (res.data.result != null) {
                      console.log(res.data.result);
                      console.log(res.data.result.question_type);
                      console.log(res.data.result.id);

                      var idd = JSON.stringify(res.data.result);
                      var id = encodeURIComponent(idd);
                      console.log(idd);
                      if (res.data.result.question_type == '选择题') {
                        wx.navigateTo({

                          url: '../test4/test4?id=' + id

                        })
                      }
                      else if (res.data.result.question_type == '判断题') {
                        wx.navigateTo({

                          url: '../test2/test2?id=' + id

                        })
                      }
                      else if (res.data.result.question_type == '填空题') {
                        wx.navigateTo({

                          url: '../test3/test3?id=' + id

                        })
                      }


                    }
                  }
                })
              }

              else {

                wx.switchTab({
                  title: '答题结束',
                  url: '../home/home'
                });


              }



            }

          })

        }
        else if (res.cancel) {
          console.log('用户点击取消')
        }
      }


    })





  }

})